package com.bupt.config.converter;

import com.bupt.base.BaseDataSupport;
import com.bupt.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.lang.Nullable;

import java.util.Date;

/**
 * 日期的转换 将前端转过来的日期字符串转换成日期的操作
 * 不然就得用 @DateTimeFormat(pattern = "yyyy-MM-dd") 在参数上
 *
 * @author :breakpoint/赵立刚
 * 2020/06/24
 * @see DateTimeFormat
 */
@Slf4j
public class MyConverter implements BaseDataSupport, Converter<String, Date> {


    @Override
    @Nullable
    public Date convert(String source) {
        log.info("com.bupt.config.converter.MyConverter.convert");
        if (null == source || "".equals(source)) {
            return null;
        }
        try {
            return BaseDataSupport.parseDateStr(source);
        } catch (BaseException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }
}
